using System;
using System.Xml.XPath;

namespace CriticalSystems.Instrumentation.LogWriters
{
    public class EventLogWriterFactory : ILogWriterFactory
    {
        public ILogWriter Create(string fullTypeName, IXPathNavigable settings)
        {
            string eventLog = "Application";
            int eventLogCategory = 0;
            if (settings != null)
            {
                XPathNavigator nav = settings.CreateNavigator();
                nav.MoveToFirstChild();
                string log = nav.GetAttribute("eventLog", string.Empty);
                if (!string.IsNullOrEmpty(log))
                {
                    eventLog = log;
                }
                string categoryText = nav.GetAttribute("eventLogCategory", string.Empty);
                if (!string.IsNullOrEmpty(categoryText))
                {
                    if (!int.TryParse(categoryText, out eventLogCategory))
                    {
                        throw new ArgumentException(Resource.EventLogWriterInvalidCategoryAttribute, "settings");
                    }
                }
            }
            return new EventLogWriter(eventLog, eventLogCategory);
        }
    }
}
